package cn.ys.org.service.impl;

import cn.ys.base.query.BaseQuery;
import cn.ys.base.service.impl.BaseServiceImpl;
import cn.ys.base.util.MD5Utils;
import cn.ys.base.util.PageList;
import cn.ys.base.util.StrUtils;
import cn.ys.org.domain.Employee;
import cn.ys.org.domain.Shop;
import cn.ys.org.mapper.EmployeeMapper;
import cn.ys.org.query.EmployeeQuery;
import cn.ys.org.service.EmployeeService;
import cn.ys.org.vo.EmpAndRoleVo;
import cn.ys.system.domain.EmployeeRole;
import cn.ys.system.domain.Role;
import cn.ys.system.mapper.EmployeeRoleMapper;
import cn.ys.system.mapper.RoleMapper;
import cn.ys.user.domain.Logininfo;
import cn.ys.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper mapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Employee> findAll() {
        return mapper.findAll();
    }

    @Override
    public Employee findById(Long id) {
        return mapper.findById(id);
    }

    // 新增员工
    @Override
    public void add(Employee employee) {
        // 添加密码
        employee.setSalt(StrUtils.getComplexRandomString(32));
        String password = MD5Utils.encrypByMd5(StrUtils.getComplexRandomString(32) + 1);
        employee.setPassword(password);
        // 添加logininfo表数据
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(1);
        logininfoMapper.add(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        // 再添加员工表
        mapper.add(employee);
        // 添加角色
        roleMapper.addMiddle(employee.getId(), employee.getRoles());

    }

    // 修改员工信息
    @Override
    public void update(Employee employee) {
        mapper.update(employee);
        // 先删除
        roleMapper.deleteByEmpId(employee.getId());
        // 再添加
        roleMapper.addMiddle(employee.getId(), employee.getRoles());
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }


    /**
     * 分页查询
     * @param query
     * @return
     * 将员工对应的角色id添加到对象中
     */
    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> pageList = super.queryByPage(query);
        // 判断是否为空
        if (pageList == null && CollectionUtils.isEmpty(pageList.getRows())) {
            return pageList;
        }
        // 1.查询所有role根据empid
        // 2.获取所有emp集合
        List<Employee> empList = pageList.getRows();
        List<Long> empIdList = empList.stream().map(Employee::getId).collect(Collectors.toList());
        // 3.根据empid集合从中间表中取出role
        List<EmpAndRoleVo> eop = roleMapper.findByEmpId(empIdList);
        // 4.对获取到的集合分组
        Map<Long, List<Long>> eopMap = eop.stream().collect(Collectors.groupingBy(EmpAndRoleVo::getEmployee_id,
                Collectors.mapping(EmpAndRoleVo::getRole_id, Collectors.toList())));
        // 5.遍历
        empList.forEach(employee -> {
            List<Long> list = eopMap.get(employee.getId());
            employee.setRoles(list);
        });
        return pageList;
    }

    @Override
    public void piDeleteById(List<Long> ids) {
        mapper.piDeleteById(ids);
    }

    @Override
    public Employee findByLogininfoId(Long logininfoId) {
        return  employeeMapper.findByLogininfoId(logininfoId);
    }
}
